#ifndef __Leakage_Detect_H
#define __Leakage_Detect_H

#ifdef APM32E10X_HD
	#include "apm32e10x_gpio.h"
	#include "apm32e10x_rcm.h"
	#include "apm32e10x_eint.h"
	#include "apm32e10x_misc.h"
	
#elif defined AT32F403AVGT7
	#include "at32f403a_407_gpio.h"
	#include "at32f403a_407_crm.h"
	#include "at32f403a_407_exint.h"
	#include "at32f403a_407_misc.h"

#else
	#include "apm32f10x_gpio.h"
	#include "apm32f10x_rcm.h"
	#include "apm32f10x_eint.h"
	#include "apm32f10x_misc.h"
#endif

#include "SQDriConfig.h"


// 漏电引脚 // 
#ifdef SQDri_EVSEB_7KW
	//引脚块1 = PD15
	#define Leakage_RTC                 RCM_APB2_PERIPH_GPIOD
	#define Leakage_PinPort             GPIOD
	#define Leakage_PinId               GPIO_PIN_15

	#if RCD_TYPE_B > 0
	// 自检引脚2 = Test In 引脚 = PD14
	#define Leak_TestIn_RTC             RCM_APB2_PERIPH_GPIOD
	#define Leak_TestIn_PinPort         GPIOD
	#define Leak_TestIn_PinId           GPIO_PIN_14

	#if testSelfLeak == Leak_FM2161A
	// 校准引脚2 = CAL 引脚 = PD13
	#define Leak_CHK_RTC                RCM_APB2_PERIPH_GPIOD
	#define Leak_CHK_PinPort            GPIOD
	#define Leak_CHK_PinId              GPIO_PIN_13
	#endif
	#endif

#elif SQDri_EVSEB_11KW
	//引脚块1 = 接引脚块A = PD15
	#define Leakage_RTC                 RCM_APB2_PERIPH_GPIOD
	#define Leakage_PinPort             GPIOD
	#define Leakage_PinId               GPIO_PIN_15
	// 外部中断
	#if RCD_Detect == RCD_Detect_EINT
	  #define Leakage_RCM_EINT          RCM_APB2_PERIPH_AFIO
		#define Leakage_EintPort          GPIO_PORT_SOURCE_D
		#define Leakage_EintPin           GPIO_PIN_SOURCE_15
		#define Leakage_EintLine          EINT_LINE_15
		#define Leakage_Eint_IRQn         EINT15_10_IRQn
		#define Leakage_Eint_Fun          EINT15_10_IRQHandler
	#endif

	#if RCD_TYPE_B > 0
	// 自检引脚2 = Test In 引脚 = PD14
	#define Leak_TestIn_RTC             RCM_APB2_PERIPH_GPIOD
	#define Leak_TestIn_PinPort         GPIOD
	#define Leak_TestIn_PinId           GPIO_PIN_14

		#if testSelfLeak == Leak_FM2161A
		// 校准引脚2 = CHK 引脚 = PD13
		#define Leak_CHK_RTC                RCM_APB2_PERIPH_GPIOD
		#define Leak_CHK_PinPort            GPIOD
		#define Leak_CHK_PinId              GPIO_PIN_13
		#endif
	#endif


#else
	//漏电引脚块1 = 接引脚块A = PA8
	#define Leakage_RTC                 RCM_APB2_PERIPH_GPIOA
	#define Leakage_PinPort             GPIOA
	#define Leakage_PinId               GPIO_PIN_8

	#if RCD_TYPE_B > 0
		// 引脚2 = Test In 引脚 = PB15
		#define Leak_TestIn_RTC             RCM_APB2_PERIPH_GPIOB
		#define Leak_TestIn_PinPort         GPIOB
		#define Leak_TestIn_PinId           GPIO_PIN_15	
		
		#if testSelfLeak == Leak_FM2161A
			// 校准引脚2 = CAL 引脚 = PB14
			#define Leak_CHK_RTC                RCM_APB2_PERIPH_GPIOB
			#define Leak_CHK_PinPort            GPIOB
			#define Leak_CHK_PinId              GPIO_PIN_14
		#endif
	#endif
	
	

#endif


// 电平设置
#if RCD_TYPE_B > 0
#if testSelfLeak == Leak_RCPD_08BS75_2P2 || testSelfLeak == UIFG_R10x_3A 
#if leakBit_level_reverse == 1 
// TestIn 设置低电平
#define leak_test_set_low()          Leak_TestIn_PinPort -> BSC  = Leak_TestIn_PinId  // 置1 
// 设置高电平
#define leak_test_set_high()         Leak_TestIn_PinPort -> BC = Leak_TestIn_PinId    // 置0
#else
// TestIn 设置低电平
#define leak_test_set_low()          Leak_TestIn_PinPort -> BC  = Leak_TestIn_PinId   // 置0 
// 设置高电平
#define leak_test_set_high()         Leak_TestIn_PinPort -> BSC = Leak_TestIn_PinId   // 置1
#endif

#elif testSelfLeak == Leak_FM2161A   
#if leakBit_level_reverse == 1       
// FMTest 设置低电平
#define leak_test_set_low()          Leak_TestIn_PinPort -> BSC = Leak_TestIn_PinId    // 置1
// FMTest 设置高电平
#define leak_test_set_high()         Leak_TestIn_PinPort -> BC  = Leak_TestIn_PinId    // 置0 
// FMCHK 设置低电平
#define leak_FMCHK_set_low()         Leak_CHK_PinPort -> BSC = Leak_CHK_PinId  
// FMCHK 设置高电平
#define leak_FMCHK_set_high()        Leak_CHK_PinPort -> BC  = Leak_CHK_PinId
#else
// FMTest 设置低电平
#define leak_test_set_low()          Leak_TestIn_PinPort -> BC   = Leak_TestIn_PinId  
// FMTest 设置高电平
#define leak_test_set_high()         Leak_TestIn_PinPort -> BSC  = Leak_TestIn_PinId   
// FMCHK 设置低电平
#define leak_FMCHK_set_low()         Leak_CHK_PinPort -> BC   = Leak_CHK_PinId  
// FMCHK 设置高电平
#define leak_FMCHK_set_high()        Leak_CHK_PinPort -> BSC  = Leak_CHK_PinId   
#endif
#endif
#endif


void leakageDetect_init(void);
#if RCD_Detect == RCD_Detect_POLL
uint8_t leakage_get_level(void);
#endif
#if RCD_Detect == RCD_Detect_EINT
void leakage_EINT_start(void);
#endif

#ifndef UsingFreeRTOS
	#if (RCD_TYPE_B > 0) && (testSelfLeak > 0)
	//B型互感器自检步骤
	void leak_test_step_run(void);
	#endif
#endif

#endif
